
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:self32/entity/book_entity.dart';
import 'package:self32/entity/print_entity.dart';

import '../../main.dart';
import '../../util/printer.dart';
import '../printing_dialog.dart';

class ResultModel{

  final RxList<BookEntity> _list = RxList();

  int type = 0;

  bool canDone = false;

  setAble(){
    canDone = true;
  }

  Widget show(list,type){
    this.type = type;
    _list.clear();
    _list.addAll(list);
    return  Obx(() => Expanded(child: Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Expanded(child: ListView.builder(
          itemBuilder: (context, index) {
            return _itemBuilder(index);
          },
          itemCount: _list.length,
        )),
        Container(
          width: double.infinity,
          height: 80.h,
          alignment: Alignment.center,
          decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/translate_bg.png",
              ),
            ),
          ),
          child: InkWell(
            onTap: ()  {
              if(!canDone){
                return;
              }
              var printList = _list.where((ele) => ele.state == 1).toList();

              if(printList.isNotEmpty && (settingEntity.print??true)){
                PrintingDialog().show((){
                  var entity = PrintEntity()
                    ..uid = currentTask.uid
                    ..uName = currentTask.uName
                    ..type = type;
                  currentTask.clear();
                  timer.cancel();
                  Get.until((route) => route.isFirst);
                  Print().printing(entity,_list.where((ele) => ele.state == 1 || ele.state ==-1).toList());
                });
              }else{
                currentTask.clear();
                timer.cancel();
                Get.until((route) => route.isFirst);
              }


            },
            child: Image.asset("assets/images/done.png",width: 340.w,fit: BoxFit.fitWidth,),
          )
        )
      ],
    )));
  }

  _itemBuilder(index){
    var item = _list[index];
    return Container(
        padding: EdgeInsets.only(top: 16.h,bottom: 16.h,left: 26.w,right: 26.w),
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/borrow_item_bg.png",
            ),
          ),
        ),
        // alignment: Alignment.center,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Image.asset("assets/images/item_icon.png",width: 10.w,fit: BoxFit.fitWidth,),
                SizedBox(width: 282.w,child: Text('《${item.mainTitle}》',style: TextStyle(color: Colors.blueAccent,fontSize: 10.sp)),),
                if(type == 0)...[
                  Image.asset("assets/images/${item.state==1?'borrow_success':item.state==2?'has_borrowed':item.state==0?'borrow_wait':'borrow_fail'}.png",width: 60.w,fit: BoxFit.fitWidth,),
                ]else...[
                  Image.asset("assets/images/${item.state==1?'return_success':item.state==2?'has_returned':item.state==0?'return_wait':'return_fail'}.png",width: 60.w,fit: BoxFit.fitWidth,),
                ]
              ],
            ),
            SizedBox(height: 5.h,),
            const Divider(height: 1.0,color: Colors.grey,),
            SizedBox(height: 10.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('图书编号：',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),
                Text('${item.no}',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),

              ],
            ),
            SizedBox(height: 5.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('应还时间：',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),
                Text(item.readyReturnDate??'',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),
              ],
            ),
            SizedBox(height: 5.h,),
            if(item.error != null)...[
              Text('失败原因：${item.error}',style: TextStyle(color: Colors.red,fontSize: 8.sp)),
              SizedBox(height: 5.h,),
            ]
          ],
        )
    );
  }
}

